<?php
namespace app\admin2\controller;

use think\Session;
use app\common\controller\AdminBaseo;
use app\common\model\Subscribereply as SubscribereplyModel;

class Subscribereply extends AdminBaseo
{
    protected $subscribereply_model;
    protected $author_config;

    protected function _initialize(){
        parent::_initialize();
        $this->author_config = session::get('wechat_info');
        $this->subscribereply_model = new SubscribereplyModel;
    }

    public function index(){
        $content = $this->subscribereply_model->where('auid',$this->author_config['id'])->value('content');
        $this->assign('content',$content);
        return $this->fetch();
    }

    public function save(){
        $data = $this->request->post();
        if($this->subscribereply_model->where('auid',$this->author_config['id'])->find()){
            $result = $this->subscribereply_model->save($data,['auid'=>$this->author_config['id']]);
        }else{
            $data['auid'] = $this->author_config['id'];
            $result = $this->subscribereply_model->allowField(true)->save($data);
        }
        
        if($result){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

    /**
     * 获取关注回复内容
     */
    public function SubscribeReply(){
        $content = $this->subscribereply_model->field('content,type')->where('auid',$this->author_config['id'])->find();
        return json(['status' => 1 , 'msg' => 'ok' , 'content' => $content]);
    }

    /**
     * 保存关注回复内容
     */
    public function saveReply(){
        $data = $this->request->post();
        if($this->subscribereply_model->where('auid',$this->author_config['id'])->find()){
            $result = $this->subscribereply_model->save($data,['auid'=>$this->author_config['id']]);
        } else {
            $data['auid'] = $this->author_config['id'];
            $result = $this->subscribereply_model->allowField(true)->save($data);
        }

        if($result){
            return json(['status' => 1 , 'msg' => 'ok' ]);
        }else{
            return json(['status' => -1 , 'error' => 'save failed']);
        }
    }
}